//
//  ViewController.m
//  20_XML_SAXAndDOM
//
//  Created by 徐昊 on 16/8/10.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, copy) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *domStuArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // SOAP协议 网络请求协议
}
// XML 分为两种解析方式
// 第一种 SAX 逐行解析 -- 使用系统的解析器 NSXMLParser
- (IBAction)saxParse:(id)sender {
    NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
    xmlParser.delegate = self;
    [xmlParser parse];
}
// 1. 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 2. 遇到节点头 方法的回调, 记录当前节点, 方便后续赋值
// 打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    NSLog(@"节点头:%@", elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [Student new];
        [_stuArray addObject:stu];
    }
}
// 3. 节点的值, 根据节点内容决定值. 没有值时对应的结果
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // string 是\n \t
//    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
    if (_currentElementName) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
        NSLog(@"节点的值:%@---节点值结束", string);
    }
//    }
}
// 4. 遇到节点尾 方法的回调, 判断当前节点的结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"节点尾:%@", elementName);
    _currentElementName = nil;
}
// 5. 解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@", _stuArray);
}
// 解析失败时, 调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"解析失败:%@", parseError);
}
// 第二种解析方式, DOM解析
// 关于GDataXMLNode的引入
// 1. 引入.h与.m文件
// 2. 引入系统动态库libxml2.tbd
// 3. 根据GDataXMLNode头文件中的提示, 添加/user/include/libxml2
// 4. 如果是ARC环境, 需要对该文件进行MRC编译, 添加-fno-objc-arc
- (IBAction)domParse:(id)sender {
    _domStuArray = [NSMutableArray array];
    NSString *xmlDataPath = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:xmlDataPath];
    NSError *error = nil;
    // 1. 创建GDataXMLDOcument对象
    // 2. 根据XML的Data数据生成一个树形结构的 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    GDataXMLElement *rootElement = [document rootElement];
    NSLog(@"%@", rootElement.name);
    // 3. 根据根节点获取 子节点数组
    NSArray *rootChildElement = rootElement.children;
    // 4. 遍历子节点数组
    for (GDataXMLElement *childElement in rootChildElement) {
//        NSLog(@"childElement name: %@", childElement.name);
        // 5. 对应所有student节点
        Student *stu = [Student new];
        // 6. 遍历student所有节点的所有子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            // 7. 赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
            [self.domStuArray addObject:stu];
        }
    }
    NSLog(@"%@", self.domStuArray);
    // XMLReader
    // 将XML结构, 转换成Dictionary
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
